package cn.huayu.edu.servlet;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FirstServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
//		ServletConfig servletConfig =  getServletConfig();
//		Enumeration<String> names =  servletConfig.getInitParameterNames();
//		while(names.hasMoreElements()) {
//			String name = names.nextElement();
//			System.out.println(name+":"+servletConfig.getInitParameter(name));
//		}
//		
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	 	
		//获取上下文ServletContext
		ServletContext context= this.getServletContext();
		Enumeration<String> names= context.getInitParameterNames();
		while (names.hasMoreElements()) {
			String name = (String) names.nextElement();
			System.out.println("context{"+name+":"+context.getInitParameter(name)+"}");
		}
		
		//获取ServletConfig
		ServletConfig config = getServletConfig();
		Enumeration<String> names2 =	config.getInitParameterNames();
		while (names2.hasMoreElements()) {
			String name = (String) names2.nextElement();
			System.out.println("config{"+name+":"+config.getInitParameter(name)+"}");
		}
		
		//设置request请求的字符集
		request.setCharacterEncoding("utf-8");
		
		System.out.println(request.getRemoteHost());
		
		//获取表单提交过来的数据
		String name = request.getParameter("name");
		String age = request.getParameter("age");
		String sex = request.getParameter("sex");
		String provice = request.getParameter("provice");
		String selfIntroduction = request.getParameter("selfIntroduction");
		String hobby[] = request.getParameterValues("hobby");
		
		//将数据放入到request Attribute ,将数据传到页面
		request.setAttribute("name", name);
		request.setAttribute("sex", sex);
		request.setAttribute("age", age);
		request.setAttribute("provice", provice);
		request.setAttribute("hobby", hobby);
		request.setAttribute("selfIntroduction", selfIntroduction);
		RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/introduction.jsp");
		dispatcher.forward(request, response);
	
	}

}
